package com.rainng.coursesystem.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rainng.coursesystem.dao.mapper.MemberMapper;
import com.rainng.coursesystem.model.entity.MemberEntity;
import com.rainng.coursesystem.model.vo.response.MemberInfoVO;
import com.rainng.coursesystem.model.vo.response.table.MemberItemVO;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class MemberDAO extends BaseDAO {
    public static final int PAGE_SIZE = 20;
    private final MemberMapper mapper;

    public MemberDAO(MemberMapper mapper) {
        this.mapper = mapper;
    }

    public MemberEntity getByNumber(String number) {
        LambdaQueryWrapper<MemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberEntity::getNumber, number);

        return mapper.selectOne(wrapper);
    }


    public int insert(MemberEntity entity) {
        return mapper.insert(entity);
    }

    public int delete(Integer id) {
        return mapper.deleteById(id);
    }

    public MemberEntity get(Integer id) {
        return mapper.selectById(id);
    }

    public int update(MemberEntity entity) {
        return mapper.updateById(entity);
    }

    public int count(String activityName, String teamName, String name) {
        return mapper.count(activityName, teamName, name);
    }

    public List<MemberItemVO> getPage(Integer index, String activityName, String teamName, String name) {
        Page<MemberItemVO> page = new Page<>(index, PAGE_SIZE);

        return mapper.getPage(page, activityName, teamName, name).getRecords();
    }

    public Integer countByTeamId(Integer id) {
        LambdaQueryWrapper<MemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberEntity::getTeamId, id);

        return mapper.selectCount(wrapper);
    }

    public List<MemberEntity> listName() {
        LambdaQueryWrapper<MemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(MemberEntity::getId, MemberEntity::getName);

        return mapper.selectList(wrapper);
    }

    public Integer getAreaIdById(Integer memberId) {
        return mapper.getAreaIdById(memberId);
    }

    public Integer getBudgetById(Integer memberId) {
        return mapper.getBudgetById(memberId);
    }

    public MemberInfoVO getMemberInfoById(Integer memberId) {
        return mapper.getMemberInfoById(memberId);
    }
}
